package com.lcg.word.screen

import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.grid.items
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.lcg.base.L
import com.lcg.base.PreferenceKTX
import com.lcg.word.Base
import com.lcg.word.BaseViewModel
import com.lcg.word.DigitalInkManager
import com.lcg.word.bean.Book
import com.lcg.word.bean.BookDb
import com.lcg.word.bean.BookUnit
import com.lcg.word.bean.bookDao
import com.lcg.word.bean.unitDao
import com.lcg.word.screen.destinations.SpellScreenDestination
import com.lcg.word.viewModel
import com.ramcosta.composedestinations.annotation.Destination
import com.ramcosta.composedestinations.navigation.DestinationsNavigator

/**
 * 书
 *
 * @author lei.chuguang Email:475825657@qq.com
 */
@Composable
@Destination
fun BookScreen(
    navigator: DestinationsNavigator,
    book: Book,
    vm: BookVM = viewModel(navigator, book.name ?: "")
) {
    LaunchedEffect(Unit) {
        vm.load(book)
    }
    Base(vm) {
        LazyVerticalGrid(
            columns = GridCells.Adaptive(200.dp),
            modifier = Modifier
                .fillMaxSize()
                .padding(16.dp, 0.dp)
        ) {
            items(vm.units) {
                val name = it.name ?: ""
                Box(
                    modifier = Modifier
                        .fillMaxWidth()
                        .height(50.dp)
                        .clickable {
                            vm.detail(it)
                        }, contentAlignment = Alignment.Center
                ) {
                    Text(text = name)
                }
            }
        }
    }
}

class BookVM : BaseViewModel() {
    var units by mutableStateOf(emptyList<BookUnit>())
    private var bookId: Long? = null

    init {
        rightText = if (PreferenceKTX.getLong("unitId") > 0) "复习已学单元" else ""
    }

    fun load(book: Book) {
        if (units.isNotEmpty()) return
        val s = book.url ?: ""
        if (s.length > 6) {
            val substring = s.substring(1..s.length - 6)
            L.i(substring)
            val bookDb = bookDao.queryBuilder().where().eq("name", substring)
                .queryForFirst()
            bookId = bookDb.id
            units = unitDao.queryForEq("book_id", bookDb.id)
        }
    }

    fun detail(unit: BookUnit) {
        if ((bookId ?: 0L) > PreferenceKTX.getLong("bookId")) {
            PreferenceKTX.setLong("bookId", bookId!!)
        }
        val id = unit.id ?: 0
        if (id > PreferenceKTX.getLong("unitId")) {
            PreferenceKTX.setLong("unitId", id)
        }
        DigitalInkManager.download(this) {
            unit.book = null
            navigator?.navigate(SpellScreenDestination(unit))
        }
    }

    override fun clickRight() {
        DigitalInkManager.download(this) {
            val unit =
                BookUnit(name = "复习：${title}", id = PreferenceKTX.getLong("unitId")).apply {
                    book = BookDb(bookId)
                }
            navigator?.navigate(SpellScreenDestination(unit))
        }
    }
}